package JFrame;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import DAO.UserDaoImpl;
import DBConn.DBConn;
import MySQL.User;

public class userMana extends JPanel implements ActionListener{
	private static final long serialVersionUID = 3531371045454656700L;
	JLabel e0,e1,e2,e3,bg;
	JTextField t1,t2,t3;
	JButton b1,home;
	Connection conn;
	public userMana(){
		conn = DBConn.open();
		ImageIcon bg1 = new ImageIcon(HomePage.class.getResource("userbg.jpg"));
		ImageIcon home1 = new ImageIcon(HomePage.class.getResource("back.png"));
		bg = new JLabel(bg1);
		e0 = new JLabel("个人信息");
		e1 = new JLabel("用户名:");
		e2 = new JLabel("原密码:");
		e3 = new JLabel("新密码:");
		t1 = new JTextField(10);
		t2 = new JTextField(10);
		t3 = new JTextField(10);
		t1 = new JTextField("");
		t2 = new JTextField("");
		t3 = new JTextField("");
		b1 = new JButton("确认修改"); 
		home = new JButton(home1);
		
		//字体设置
		Font f = new Font("隶书",Font.BOLD,23);
		e0.setFont(f);
		e1.setFont(f);t1.setFont(f);
		e2.setFont(f);t2.setFont(f);
		e3.setFont(f);t3.setFont(f);
		b1.setFont(f);b1.setForeground(Color.blue);
		//位置设置
		this.setLayout(null); // 绝对布局
		e0.setBounds(340,65,110,30);
		e1.setBounds(240, 120,110,30); t1.setBounds(345, 120,200, 30);
		e2.setBounds(240, 180,110,30); t2.setBounds(345, 180,200, 30);
		e3.setBounds(240, 240,110,30); t3.setBounds(345, 240,200, 30);
		b1.setBounds(340, 300,140,30); bg.setBounds(0, 0, 800, 600);
		home.setBounds(1, 1, 64, 64);
		
		add(e0);
		add(e1);add(t1);
		add(e2);add(t2);
		add(e3);add(t3);
		add(b1);add(home);
		home.setOpaque(false);//按钮透明
		home.setBorderPainted(false); // 按钮无边框
		this.add(bg); //背景，要在最后add不然会遮住一些组件
		//按钮监听
		this.b1.addActionListener(this);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==this.b1){
			if(t1.getText().equals("")||t2.getText().equals("")||t3.getText().equals("")){
				JOptionPane.showMessageDialog(null,"请输入完整信息！","Erro",JOptionPane.ERROR_MESSAGE);
			}
			else{
				User u = new User();
				UserDaoImpl udi = new UserDaoImpl();
				try{
					u = udi.getUserById(t1.getText());
					if(t2.getText().equals(u.getPassword())){  //这里我没有判断用户类型
					u.setPassword(t3.getText());
					JOptionPane.showMessageDialog(null,"修改成功!","Nice",JOptionPane.OK_OPTION);
					}		
				}catch (Exception e1) {
					JOptionPane.showMessageDialog(null,"用户名或密码错误","Erro",JOptionPane.YES_NO_CANCEL_OPTION);
					t1.requestFocus();
				}
			}
		}
	}
}
